<?php

namespace addons\xpframework\library\xcore\xcore\utils;

use fast\Pinyin;


class DevXcFaUtils
{
    /**
     * add_menu_function(1, "xcflow", [["mdmconfig", "配置"]]);
     * @return void
     */
    public static function add_menu_function($pid, $controlpath, $new_menus)
    {
        $new_menus[] = ["index","查询"];
        $new_menus[] = ["add","创建"];
        $new_menus[] = ["edit","修改"];
        $new_menus[] = ["del","删除"];
        $new_menus[] = ["multi","批量"];


        $dao = new \app\admin\model\AuthRule();
        foreach ($new_menus as $new) {
            $new_name = XcConfigUtils::getAddonCode() . "/{$controlpath}/" . $new[0];
            $row = XcDaoUtils::getRowBySql($dao, [['name', '=', $new_name]]);
            if ($row == null) {
                $row = XcDaoUtils::create($dao, [
                    "type" => "file",
                    "pid" => $pid,
                    "name" => $new_name,
                    "title" => $new[1],
                    "icon" => "fa fa-circle-o",
                    "ismenu" => 0,
                    'status' => 'normal',
                    'py' => Pinyin::get($new[1], true),
                    'pinyin' => Pinyin::get($new[1]),
                    "weigh" => 0,
                ]);

                pp("创建菜单: #{$row->id} {$new[0]}");
            } else {
                pp("菜单已经存在, 跳过: #{$row->id} {$new[0]}");
            }
        }
    }
}
